package swing.chapter03;

import swing.util.SwingConsle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TicTacToe extends JFrame {
    private JTextField rows = new JTextField("3");
    private JTextField cols = new JTextField("3");

    private enum State {BLANK, XX, OO}

    static class ToeDialog extends JDialog{
        private State turn = State.XX;

        public ToeDialog(int cellsWide, int cellsHigh){
            setTitle("The game itself");
            setLayout(new GridLayout(cellsWide, cellsHigh));
            for(int i=0;i<cellsWide*cellsHigh;i++){
                add(new ToeButton());
            }

            setSize(cellsWide * 50, cellsHigh * 50);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }

        class ToeButton extends JPanel {
            private State state = State.BLANK;

            public ToeButton(){
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        if(state == State.BLANK){
                            state = turn;
                            turn = (turn == State.XX) ? State.OO : State.XX;
                        }else{
                            state = (state == State.XX) ? State.OO : State.XX;
                        }

                        repaint();
                    }
                });
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                int x1 = 0, y1 = 0, x2 = getSize().width - 1, y2 = getSize().height - 1;
                g.drawRect(x1,y1,x2,y2);
                x1 = x2/4; y1 = y2/4;

                int wide = x2/2, high = y2/2;
                if(state == State.XX){
                    g.drawLine(x1, y1, x1 + wide, y1 + high);
                    g.drawLine(x1, y1 + high, x1 + wide, y1);
                }
                if(state == State.OO){
                    g.drawOval(x1, y1, x1 + wide/2, y1 + high/2);
                }
            }
        }// end of ToeButton

    } // end of TaeDialog

    public TicTacToe(){
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2,2));
        p.add(new JLabel("Rows", JLabel.CENTER));
        p.add(rows);
        p.add(new JLabel("Colums", JLabel.CENTER));
        p.add(cols);
        add(p, BorderLayout.NORTH);

        JButton go = new JButton("go");
        go.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog d = new ToeDialog(
                        new Integer(rows.getText()),
                        new Integer(cols.getText())
                );
                d.setVisible(true);
            }
        });
        add(go, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingConsle.run(new TicTacToe(), 200,200);
    }

}
